package com.sc1.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

//mybatis工具类，目前用于mybatis测试使用
//以后SSM(Spring Springmvc Mybatis)整合之后  或者通过Springboot
public class MybatisUtil {
    private static SqlSessionFactory sf;
    private static SqlSession session;
    static {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis.xml");
            sf = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static <A> A getMapper(Class<A> c) {
        session = sf.openSession();
        return session.getMapper(c);
    }
    public static void close(){
        session.commit();
        session.close();
    }
}
